<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2022/12/8
 * Time: 9:40
 */

namespace app\controller;


class Upload
{
    public function image(){
        $image = input('image');
        if (!empty($image)){
            $image = explode(',',$image)[1];
            $image = base64_decode($image);
            !is_dir('images') && mkdir('images');
            !is_dir('images/'.date('Ymd')) && mkdir('images/'.date('Ymd'));
            $file = 'images/'.date('Ymd').'/'.time().'.jpg';
            file_put_contents($file,$image);
            return json(['code' => 200,'msg' => '','data' =>  '/'.$file]);
        }else {
            $file = request()->file('file');
            // 上传到本地服务器
            $savename = \think\facade\Filesystem::putFile('topic', $file, 'public');
            $savename = str_replace('\\', '/', $savename);
            return json(['code' => 200,'msg' => '','data' =>  '/storage/'.$savename]);
        }
    }
}